/**
 * Created by 张其川 on 2016/7/10.
 */



function createServerLine(serverLine) {
    //serverLine是一个服务器的全部配置
    var tbody = document.getElementById("serversConfigTbody");
    var tr = document.createElement("tr");//创建一行
    var sid = serverLine.id;
    tr.setAttribute("sid", sid);

    var td = document.createElement("td");
    td.textContent = serverLine.sid;
    td.className = "sid";
    tr.appendChild(td);

    //第二个字段IP
    var td = document.createElement("td");
    td.textContent = serverLine.name;
    td.className = "name";
    tr.appendChild(td);

    //第三个字段是别名
    var td = document.createElement("td");
    td.textContent = serverLine.subject;
    td.className = "subject";
    tr.appendChild(td);

    //第四个字段属主
    var td = document.createElement("td");
    td.className = "experiment";
    td.textContent = serverLine.experiment;
    tr.appendChild(td);

    //第五个字段主机组
    var td = document.createElement("td");
    td.className = "date";
    td.textContent = serverLine.date;
    tr.appendChild(td);
    //第五个字段 用户名
    var td = document.createElement("td");
    td.className = "classNumber";
    td.textContent = serverLine.class_number;
    tr.appendChild(td);


    //第十个字段端口
    var td = document.createElement("td");
    td.className = "description"
    td.textContent = serverLine.description;
    tr.appendChild(td);

    //第十六个字段 操作

    //编辑按钮
    var td = document.createElement("td");
    var editButton = document.createElement("button");
    editButton.className = "btn btn-success btn-xs glyphicon glyphicon-edit";
    editButton.setAttribute("sid", serverLine.sid);//服务器的每一个参数在className中
    editButton.setAttribute("data", JSON.stringify(serverLine));//服务器的每一个参数在className中
    editButton.onclick = function () {
        window.currentEditTr = this;//当前最的编辑行
        //把sid传递给保存按钮
        document.getElementById("saveServerConfig").setAttribute("sid", this.getAttribute("sid"));
        loadServerLineToTable(this);//把当前的编辑按钮传入，按钮中存在各个属性
        window.currentEditServerModel = "edit";//记录当前的编辑模式，有edit/create
    }
    td.appendChild(editButton);
    //删除按钮
    var deleteButton = document.createElement("button");
    deleteButton.style.marginLeft = "3px";
    deleteButton.className = "btn btn-danger btn-xs glyphicon glyphicon-trash";
    deleteButton.setAttribute("sid", serverLine.sid);
    deleteButton.onclick = function () {
        //记得从windowallServerList中删除记录
        var deleteButtonElements = [this];//可以批量删除
        deleteServerConfig(deleteButtonElements);
    }
    td.appendChild(deleteButton);
    tr.appendChild(td);


    var sendButton = document.createElement("button");
    sendButton.style.marginLeft = "3px";
    sendButton.className = "btn btn-success btn-xs glyphicon glyphicon-envelope";
    sendButton.setAttribute("sid", serverLine.sid);
    sendButton.onclick = function () {
        //记得从windowallServerList中删除记录
        window.open ("students.html", "a", "height='100%', width='100%', top=0, left=0, toolbar=no, menubar=no, scrollbars=yes, resizable=no, location=no, status=no");
    }
    td.appendChild(sendButton);
    tr.appendChild(td);

    //最后加入表格
    tbody.appendChild(tr);
}

document.getElementById("createExp").onclick=function(){
        window.open ("exp.html?action=POST", "_blank", "height=600, width=900, top=50, left=20, toolbar=no, menubar=no, scrollbars=yes, resizable=no, location=no, status=no");
	
}
function getPublishTestList(){
	jQuery.ajax({
		"url":"/publish_test/",
		"dataType":"jsonp",
		"error":errorAjax,
		"beforeSend":start_load_pic,
		"complete":stop_load_pic,
		"success":function(data){
			if(!data.status){
				showErrorInfo(data.content)
				return false;
			}
			else{
				var tab=document.getElementById("tab");
				data=data.content
				for(var i=0;i<data.length;i++){
					var tr=document.createElement("tr")
					tr.setAttribute("data",JSON.stringify(data.i))

					var td=document.createElement("td")
					td.textContent=data[i].classname;
					tr.appendChild(td)
					
					var td=document.createElement("td")
					td.textContent=data[i].content;
					tr.appendChild(td)
					
					var td=document.createElement("td")
					td.textContent=data[i].test_name;
					tr.appendChild(td)
					
					var td=document.createElement("td")
					td.textContent=data[i].date;
					tr.appendChild(td)
					
    var td = document.createElement("td");
    var editButton = document.createElement("button");
    editButton.className = "btn btn-success btn-xs glyphicon glyphicon-edit";
    editButton.setAttribute("sid", data[i].id);//服务器的每一个参数在className中
    editButton.setAttribute("data", JSON.stringify(data[i]));//服务器的每一个参数在className中
    editButton.onclick = function () {
	var data=this.getAttribute("data")
        window.open ("exp.html?action=PUT&data="+data, "_blank", "height=600, width=900, top=50, left=20, toolbar=no, menubar=no, scrollbars=yes, resizable=no, location=no, status=no");
    }
    //td.appendChild(editButton);
    var deleteButton = document.createElement("button");
    deleteButton.style.marginLeft = "3px";
    deleteButton.className = "btn btn-danger btn-xs";
    deleteButton.textContent="删除"
    deleteButton.setAttribute("sid", data[i].id);
    deleteButton.onclick = function () {
		if(confirm("真的要删除吗？")===false){
			return false;
		}
		delPublishTest(this)
    }
    td.appendChild(deleteButton);
    var vButton = document.createElement("a");
    vButton.style.marginLeft = "3px";
    vButton.className = "btn btn-primary btn-xs";
	vButton.textContent="学生列表"
    vButton.setAttribute("href","cheungssh.html?page=view_expandstudent.html&id="+data[i].id)

    td.appendChild(vButton);
    tr.appendChild(td);


			tab.appendChild(tr)
		}
	}
}
})
}
function delPublishTest(e){
	var sid=e.getAttribute("sid")
	var data={}
	data["sid"]=sid
	data=JSON.stringify(data)
	jQuery.ajax({
		"url":"/publish_test/",
		"type":"DELETE",
		"beforeSend":start_load_pic,
		"complete":stop_load_pic,
		"error":errorAjax,
		"data":{"data":data},
		"success":function(data){
			data=JSON.parse(data)
			if(!data.status){
				showErrorInfo(data.content)
				return false;
			}
			else{
				showSuccessNotic();
				$(e).parent().parent().remove()
			}
		}
	})
}
getPublishTestList();
